<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>最长子序列</title>
</head>
<body>
    <div>
        输入两组字母：abcadf、abcad
    </div>
    <br>
    <br>
    <br>
    <p>请按一下按钮</p>
    <br>
    <br>
  <script>
      function lstd(stra,strb){
    var n,i,j;
    var dp=[],lst=[];
    var max=function(a,b){return (a>b?a:b);};
    var arr=stra.split("");
    var brr=strb.split("");
    var lena=arr.length;
    var lenb=brr.length;

    for(i=0;i<lena+1;i++){
        dp[i]=[];
        for(j=0;j<lenb+1;j++){
        if(i==0||j==0) dp[i][j]=0;
        else if(arr[i]===brr[j]){dp[i][j]=dp[i-1][j-1]+1;}
        else{dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}
        }
    }
    var lst="";
    var traceback=function(i,j,lst){

        while(i>0&&j>0){
        if(arr[i-1]===brr[j-1]){
        lst+=arr[i-1];
        if(lst.length===dp[lena][lenb]){
    console.log(lst.split("").reverse().join(""));}
        i--;j--;
        }
        else{
            if(dp[i-1][j]>dp[i][j-1])
            {--i;}
            else if(dp[i-1][j]<dp[i][j-1])
            {--j;}
            else{
            traceback(i-1,j,lst);
            traceback(i,j-1,lst);
            return;
            }
        }
        }
    };
    traceback(lena,lenb,lst);
    return dp[lena][lenb];
}

x="abcadf";y="abcad";

/*function enter()
{
    var arr1= [];
    var arr2= [];
    arr1.length=10;
    arr2.length=10;
    for (var i=0;i<arr1.length;i++)
    {
        array.push()
    }
}
*/
function show()
{
    alert(lstd(x,y))
}

</script>
<input type="button" value="确定" onclick="show()">
</style>
</body>
</html>